package cn.techwis.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.techwis.dto.PayBillDTO;
import cn.techwis.dto.PayRefundQueryDTO;
import cn.techwis.dto.QueryOrderDTO;
import cn.techwis.dto.RefundsDTO;
import cn.techwis.vo.InItVO;

import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Validation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 父类 service
 */
public class Tservice {

    /**
     * 参数校验
     */
    protected static final void validateObject(Object obj, Class cl, InItVO inItVO) {
        Set<ConstraintViolation<Object>> validate = Validation.buildDefaultValidatorFactory().getValidator().validate(obj, cl);
        Set<ConstraintViolation<Object>> validate1 = Validation.buildDefaultValidatorFactory().getValidator().validate(inItVO, InItVO.init.class);
        validate.addAll(validate1);
        HashMap<String, List<String>> hashMap = new HashMap();
        validate.stream().forEach(s -> {
            Path propertyPath = s.getPropertyPath();
            String messageTemplate = s.getMessageTemplate();
            String name = s.getRootBeanClass().getName();
            List<String> list = hashMap.get(name);
            if (CollectionUtil.isNotEmpty(list)) {
                list.add(propertyPath + ":" + messageTemplate);
                hashMap.put(name, list);
            } else {
                list = new ArrayList<>();
                list.add(propertyPath + ":" + messageTemplate);
                hashMap.put(name, list);
            }
        });

        List<String> collect = validate.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(collect)) {
            throw ExceptionUtil.wrapRuntime(JSONUtil.toJsonStr(hashMap));
        }
    }


    protected static final void validateObject(Object obj, Class cl) {
        Set<ConstraintViolation<Object>> validate = Validation.buildDefaultValidatorFactory().getValidator().validate(obj, cl);
        List<String> collect = validate.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(collect)) {
            throw ExceptionUtil.wrapRuntime(CollectionUtil.join(collect, ","));
        }

    }

    /**
     * JASPI/小程序/APP下单结果处理
     */
    protected static String payResult(JSONObject jsonObject) {
        String prepay_id = jsonObject.getStr("prepay_id");
        if (StrUtil.isNotEmpty(prepay_id)) {
            return prepay_id;
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }

    /**
     * H5下单结果处理
     *
     * @param jsonObject
     * @return
     */
    protected static String payH5Result(JSONObject jsonObject) {
        String h5_url = jsonObject.getStr("h5_url");
        if (StrUtil.isNotEmpty(h5_url)) {
            return h5_url;
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }

    /**
     * NATIVE下单结果处理
     *
     * @param jsonObject
     * @return
     */
    protected static String payNativeResult(JSONObject jsonObject) {
        String code_url = jsonObject.getStr("code_url");
        if (StrUtil.isNotEmpty(code_url)) {
            return code_url;
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }


    /**
     * 查询订单结果处理
     */
    protected static QueryOrderDTO queryResult(JSONObject jsonObject) {
        String transaction_id = jsonObject.getStr("transaction_id");
        if (StrUtil.isNotEmpty(transaction_id)) {
            return JSONUtil.toBean(jsonObject, QueryOrderDTO.class);
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }

    /**
     * 关闭订单结果处理
     */
    protected static Boolean closeResult(JSONObject jsonObject) {
        String reponseStatus = jsonObject.getStr("reponseStatus");
        if (reponseStatus.equals("204")) {
            //处理成功
            return true;
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }

    /**
     * 退款结果处理
     */
    protected static RefundsDTO refundsResult(JSONObject jsonObject) {
        String transaction_id = jsonObject.getStr("transaction_id");
        if (StrUtil.isNotEmpty(transaction_id)) {
            //处理成功
            RefundsDTO refundsDTO = JSONUtil.toBean(jsonObject, RefundsDTO.class);
            return refundsDTO;
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }

    /**
     * 退款结果查询处理
     */
    protected static PayRefundQueryDTO refundsQueryResult(JSONObject jsonObject) {
        String refund_id = jsonObject.getStr("refund_id");
        if (StrUtil.isNotEmpty(refund_id)) {
            //处理成功
            PayRefundQueryDTO payRefundQueryDTO = JSONUtil.toBean(jsonObject, PayRefundQueryDTO.class);
            return payRefundQueryDTO;
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }

    /**
     * 对账单结果处理
     */
    protected static PayBillDTO billQueryResult(JSONObject jsonObject) {
        String download_url = jsonObject.getStr("download_url");
        if (StrUtil.isNotEmpty(download_url)) {
            //处理成功
            PayBillDTO payRefundQueryDTO = JSONUtil.toBean(jsonObject, PayBillDTO.class);
            return payRefundQueryDTO;
        } else {
            throw ExceptionUtil.wrapRuntime(jsonObject.toString());
        }
    }

}
